package com.yungu.swift.socket.communication.server.handler.strategy;

import com.google.gson.Gson;
import com.yungu.swift.socket.communication.message.PushResponseMessage;
import com.yungu.swift.socket.component.PushContentMap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

/**
 * 推送响应
 *
 * @author : cuixiuyin
 * @date : 2019/8/30
 */
public class PushHandler implements MessageHandler {

    @Override
    public void handler(ChannelHandlerContext channelHandlerContext, String text) {
        Gson gson = new Gson();
        PushResponseMessage pushResponseMessage = gson.fromJson(text, PushResponseMessage.class);
        if (PushContentMap.getInstance().containsKey(pushResponseMessage.getPushUuid())) {
            PushContentMap.getInstance().remove(pushResponseMessage.getPushUuid());
            pushResponseMessage.setSuccess(true);
            pushResponseMessage.setMsg("收到回复");
            channelHandlerContext.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(pushResponseMessage)));
        }
    }
}
